package org.jeecg.modules.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.dto.InAddModelDto;
import org.jeecg.modules.dto.InCopyModelDto;
import org.jeecg.modules.service.IActModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 流程模板
 * @Author: zhouwr
 * @Date: 2023-12-01
 * @Version: V1.0
 */
@Api(tags = "流程模板")
@RestController
@RequestMapping("/act/model")
@Slf4j
public class ActModelController extends JeecgController<InAddModelDto, IActModelService> {
  @Autowired
  public RepositoryService repositoryService;
  @Autowired
  private IActModelService actModelService;

  /**
   * 分页列表查询
   *
   * @param model
   * @param pageNo
   * @param pageSize
   * @param req
   * @return
   */
  // @AutoLog(value = "流程模板-分页列表查询")
  @ApiOperation(value = "流程模板-分页列表查询", notes = "流程模板-分页列表查询")
  @GetMapping(value = "/listPage")
  public Result<IPage<Model>> listPage(Model model,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       HttpServletRequest req) {
    IPage<Model> pageList = actModelService.page(model, pageNo, pageSize);
    return Result.OK(pageList);
  }

  @ApiOperation(value = "历史流程模板-分页列表查询", notes = "历史流程模板-分页列表查询")
  @GetMapping(value = "/listHistoryPage")
  public Result<IPage<Model>> listHistoryPage(Model model,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       HttpServletRequest req) {
    IPage<Model> pageList = actModelService.historyPage(model, pageNo, pageSize);
    return Result.OK(pageList);
  }

  @RequiresPermissions("act:model:deploy")
  @GetMapping(path = "/deploy")
  @ApiOperation(value = "根据modelId部署流程", notes = "根据modelId部署流程")
  public Result<String> deploy(@RequestParam("id") String id, @RequestParam("name") String name) {
    String msg = actModelService.deploy(id, name);
    return Result.OK(msg);
  }

  @RequiresPermissions("act:model:exportXml")
  @GetMapping(path = "/exportXml")
  @ApiOperation(value = "导出流程xml", notes = "根据modelId导出流程xml")
  public void exportXml(@RequestParam("id") String id, HttpServletResponse response) {
    actModelService.exportXml(id, response);
  }

  @RequiresPermissions("act:model:exportXml")
  @GetMapping(path = "/getModelXml")
  @ApiOperation(value = "获取流程xml内容", notes = "根据modelId获取流程xml内容")
  public Result<Map<String, String>> getModelXml(@RequestParam("id") String id) {
    final Map<String, String> map = new HashMap<>();
    map.put("xmlContent", actModelService.getModelXml(id));
    return Result.OK("成功获取model的xml内容", map);
  }

  /**
   * 添加
   *
   * @param inAddModelDto
   * @return
   */
  @AutoLog(value = "流程模板-添加")
  @ApiOperation(value = "流程模板-添加", notes = "流程模板-添加")
  @RequiresPermissions("act:model:add")
  @PostMapping(value = "/add")
  public Result<String> add(@RequestBody InAddModelDto inAddModelDto) {
      final String msg;
      try {
          msg = actModelService.create(inAddModelDto);
      } catch (TranscoderException | IOException e) {
          return Result.error(e.getMessage());
      }
      return Result.OK(msg);
  }

    @AutoLog(value = "流程模板-复制")
    @ApiOperation(value = "流程模板-复制", notes = "流程模板-复制")
    @RequiresPermissions("act:model:copy")
    @PostMapping(value = "/copy")
    public Result<String> copy(@RequestBody InCopyModelDto inCopyModelDto) {
        try {
            actModelService.modelCopy(inCopyModelDto);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.OK("流程模型复制成功");
    }

  /**
   * 编辑
   *
   * @param inAddModelDto
   * @return
   */
  @AutoLog(value = "流程模板-编辑")
  @ApiOperation(value = "流程模板-编辑", notes = "流程模板-编辑")
  @RequiresPermissions("act:model:edit")
  @RequestMapping(value = "/edit", method = { RequestMethod.PUT, RequestMethod.POST })
  public Result<String> edit(@RequestBody InAddModelDto inAddModelDto) {
    actModelService.updateById(inAddModelDto);
    return Result.OK("编辑成功!");
  }

  /**
   * 通过id删除
   *
   * @param id
   * @return
   */
  @AutoLog(value = "流程模板-通过id删除")
  @ApiOperation(value = "流程模板-通过id删除", notes = "流程模板-通过id删除")
  @RequiresPermissions("act:model:delete")
  @DeleteMapping(value = "/delete")
  public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
    actModelService.delete(id);
    return Result.OK("删除成功!");
  }

  /**
   * 批量删除
   *
   * @param ids
   * @return
   */
  @AutoLog(value = "流程模板-批量删除")
  @ApiOperation(value = "流程模板-批量删除", notes = "流程模板-批量删除")
  @RequiresPermissions("act:model:deleteBatch")
  @DeleteMapping(value = "/deleteBatch")
  public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
    this.actModelService.removeByIds(Arrays.asList(ids.split(",")));
    return Result.OK("批量删除成功!");
  }

  /**
   * 通过id查询
   *
   * @param id
   * @return
   */
  // @AutoLog(value = "流程模板-通过id查询")
  @ApiOperation(value = "流程模板-通过id查询", notes = "流程模板-通过id查询")
  @GetMapping(value = "/queryById")
  public Result<InAddModelDto> queryById(@RequestParam(name = "id", required = true) String id) {
    InAddModelDto inAddModelDto = actModelService.getById(id);
    if (inAddModelDto == null) {
      return Result.error("未找到对应数据");
    }
    return Result.OK(inAddModelDto);
  }

  /**
   * 导出excel
   *
   * @param request
   * @param inAddModelDto
   */
  @RequiresPermissions("act:model:exportXls")
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, InAddModelDto inAddModelDto) {
    return super.exportXls(request, inAddModelDto, InAddModelDto.class, "流程模板");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequiresPermissions("act:model:importExcel")
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
    return super.importExcel(request, response, InAddModelDto.class);
  }

}
